using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShivamEMR.Model.Domain.CompanyDomain;
using ShivamEMR.Model.DataInterfaces.PersonDataInterf;
using ShivamEMR.Model.DataInterfaces.CompanyDataInterf;
using ShivamEMR.Data.Repositories.PersonRepo;
using ShivamEMR.Data.Repositories.CompanyRepo;
using ShivamEMR.Model;
using ShivamEMR.Web.Models;
using PagedList;
//using ShivamEMR.Web.Areas.Company.Models;

namespace ShivamEMR.Web.Areas.Company.Controllers
{   
    public class EmployeeController : Controller
    {
		private readonly ISexRepository sexRepository;
		private readonly IPersonPictureRepository personpictureRepository;
		private readonly IPersonAccountRepository personaccountRepository;
		private readonly IPersonStatusRepository personstatusRepository;
		private readonly IEmployeeStatusRepository employeestatusRepository;
		private readonly IEmployeeGroupRepository employeegroupRepository;
		private readonly IEmployeeRepository employeeRepository;
        private readonly IMaritalStatusRepository maritalStatusRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public EmployeeController() 
        {
            sexRepository = Registry.RepositoryFactory.GetSexRepository();
            personpictureRepository = Registry.RepositoryFactory.GetPersonPictureRepository();
            personaccountRepository = Registry.RepositoryFactory.GetPersonAccountRepository();
            personstatusRepository = Registry.RepositoryFactory.GetPersonStatusRepository();
            employeestatusRepository = Registry.RepositoryFactory.GetEmployeeStatusRepository();
            employeegroupRepository = Registry.RepositoryFactory.GetEmployeeGroupRepository();
            employeeRepository = Registry.RepositoryFactory.GetEmployeeRepository();
            maritalStatusRepository = Registry.RepositoryFactory.GetMaritalStatusRepository();
        }

        public EmployeeController(ISexRepository sexRepository, IPersonPictureRepository personpictureRepository, IPersonAccountRepository personaccountRepository, IPersonStatusRepository personstatusRepository, IEmployeeStatusRepository employeestatusRepository, IEmployeeGroupRepository employeegroupRepository, IEmployeeRepository employeeRepository, IMaritalStatusRepository maritalstatusRepo)
        {
			this.sexRepository = sexRepository;
			this.personpictureRepository = personpictureRepository;
			this.personaccountRepository = personaccountRepository;
			this.personstatusRepository = personstatusRepository;
			this.employeestatusRepository = employeestatusRepository;
			this.employeegroupRepository = employeegroupRepository;
			this.employeeRepository = employeeRepository;
            this.maritalStatusRepository = maritalstatusRepo;
        }

        //
        // GET: /Employee/
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ViewResult Index(string searchString, int? page)
        {
            var employees = employeeRepository.All;
            if (!String.IsNullOrEmpty(searchString))
            {
                employees = employees.Where(p => p.FirstName.ToUpper().Contains(searchString.ToUpper())
                                   || p.MiddleName.ToUpper().Contains(searchString.ToUpper())
                                   || p.LastName.ToUpper().Contains(searchString.ToUpper())
                    || p.Code.ToUpper().Contains(searchString.ToUpper()));
            }
            var plist = employees.ToList<ShivamEMR.Model.Domain.CompanyDomain.Employee>();
            int pageSize = 10;
            int pageNumber = (page ?? 1);
            return View(plist.ToPagedList(pageNumber, pageSize)); 
            //return View(employeeRepository.AllIncluding(employee => employee.Sex, employee => employee.Picture, employee => employee.PersonStatus, employee => employee.Addresses, employee => employee.Contacts, employee => employee.CitizenShips, employee => employee.Informations, employee => employee.PersonRoles, employee => employee.Groups, employee => employee.Documents, employee => employee.Assignments, employee => employee.InteractionsWith, employee => employee.EmployeeStatus, employee => employee.CurrentBranches, employee => employee.Designations, employee => employee.Appointments, employee => employee.Schedule, employee => employee.Interactions));
            //return View(employeeRepository.GetAll());
        }

        //
        // GET: /Employee/Details/5
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ViewResult Details(System.Guid id)
        {
            return View(employeeRepository.Find(id));
        }

        //
        // GET: /Employee/Create
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ActionResult Create()
        {
			ViewBag.PossibleSexes = sexRepository.All;
			ViewBag.PossiblePictures = personpictureRepository.All;
			ViewBag.PossiblePersonAccounts = personaccountRepository.All;
			ViewBag.PossiblePersonStatus = personstatusRepository.All;
			ViewBag.PossibleEmployeeStatus = employeestatusRepository.All;
			ViewBag.PossibleEmployeeGroups = employeegroupRepository.All;
            var ms = maritalStatusRepository.All;
            ViewBag.PassibleMaritalStatus = maritalStatusRepository.All;
            var emp = new ShivamEMR.Model.Domain.CompanyDomain.Employee() { DateOfBirth=DateTime.Now, DateOfJoin=DateTime.Now };
            return View(emp);
        } 

        //
        // POST: /Employee/Create
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        [HttpPost]
        public ActionResult Create(Employee employee)
        {
            if (ModelState.IsValid) {
                employeeRepository.Create(employee);
                employeeRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleSexes = sexRepository.All;
				ViewBag.PossiblePictures = personpictureRepository.All;
				ViewBag.PossiblePersonAccounts = personaccountRepository.All;
				ViewBag.PossiblePersonStatus = personstatusRepository.All;
				ViewBag.PossibleEmployeeStatus = employeestatusRepository.All;
				ViewBag.PossibleEmployeeGroups = employeegroupRepository.All;
                ViewBag.PassibleMaritalStatus = maritalStatusRepository.All;
				return View();
			}
        }
        
        //
        // GET: /Employee/Edit/5
 [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ActionResult Edit(System.Guid id)
        {
			ViewBag.PossibleSexes = sexRepository.All;
			ViewBag.PossiblePictures = personpictureRepository.All;
			ViewBag.PossiblePersonAccounts = personaccountRepository.All;
			ViewBag.PossiblePersonStatus = personstatusRepository.All;
			ViewBag.PossibleEmployeeStatus = employeestatusRepository.All;
			ViewBag.PossibleEmployeeGroups = employeegroupRepository.All;
            ViewBag.PassibleMaritalStatus = maritalStatusRepository.All;
             return View(employeeRepository.Find(id));
        }

        //
        // POST: /Employee/Edit/5
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        [HttpPost]
        public ActionResult Edit(Employee employee)
        {
            if (ModelState.IsValid) {
                employeeRepository.Update(employee);
                employeeRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleSexes = sexRepository.All;
				ViewBag.PossiblePictures = personpictureRepository.All;
				ViewBag.PossiblePersonAccounts = personaccountRepository.All;
				ViewBag.PossiblePersonStatus = personstatusRepository.All;
				ViewBag.PossibleEmployeeStatus = employeestatusRepository.All;
				ViewBag.PossibleEmployeeGroups = employeegroupRepository.All;
                ViewBag.PassibleMaritalStatus = maritalStatusRepository.All;
				return View();
			}
        }

        //
        // GET: /Employee/Delete/5
 [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ActionResult Delete(System.Guid id)
        {
            return View(employeeRepository.Find(id));
        }

        //
        // POST: /Employee/Delete/5
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(System.Guid id)
        {
            var emp = employeeRepository.Find(id);
            employeeRepository.Delete(emp);
            employeeRepository.Save();

            return RedirectToAction("Index");
        }
    }
}

